package com.jadcon.bedbreakfast.domain;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class PhoneNumberValidator implements Validator {

	@SuppressWarnings("unchecked")
	public boolean supports(Class clazz) {
		return PhoneNumber.class.equals(clazz);
	}

	public void validate(Object object, Errors e) {
		PhoneNumber phoneNumber = (PhoneNumber) object;
		int areaCode = phoneNumber.getAreaCode();
		int exchange = phoneNumber.getExchange();
		int lineNumber = phoneNumber.getLineNumber();
		
		if (areaCode == 0 || areaCode>999) {
			e.rejectValue("areaCode", "phoneNumber.invalid");
		}
		else if (exchange == 0 || exchange>999) {
			e.rejectValue("exchange", "phoneNumber.invalid");
		}
		else if (lineNumber == 0 || lineNumber>9999) {
			e.rejectValue("lineNumber", "phoneNumber.invalid");
		}
	}
}
